package com.blb.trans.util;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

@Aspect
@EnableAspectJAutoProxy
@Configuration
public class TransactionAspect {

    //注入事务管理器
    @Autowired
    private DataSourceTransactionManager transactionManager;

//    @Around("execution(* com.blb.trans.service.impl.*ServiceImpl.*(..))")
    public void handleTransaction(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("启动事务");
        //启动事务
        TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
        //执行原来的方法
        joinPoint.proceed();
        //提交事务
        transactionManager.commit(status);
        System.out.println("提交事务");
    }
}
